import { UseGuards, Injectable } from '@nestjs/common';
import { UsersEntity } from '../users/users.entity';
import { Role } from '../role/entities/role.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { CreateAdminuserDto } from './dto/create-adminuser.dto';
import { UpdateAdminuserDto } from './dto/update-adminuser.dto';

@Injectable()
export class AdminuserService {
  constructor(
    @InjectRepository(UsersEntity)
    private readonly usersRepository: Repository<UsersEntity>,
    @InjectRepository(Role)
    private readonly roleRepository: Repository<Role>,
  ) {}

  create(createAdminuserDto: CreateAdminuserDto) {
    return 'This action adds a new adminuser';
  }

  async findAll() {
    const qb = await this.usersRepository
      .createQueryBuilder('u')
      .leftJoinAndSelect('u.role', 'r')
      .getMany();

    // console.log(qb);
    return qb;
    /*if (zd) {
                                                                                                                      qb = qb.where('article.name like :name', { name: '%' + zd + '%' });
                                                                                                                    }
                                                                                                                    qb = qb.skip(pageSize * (pageNum - 1)).take(pageSize);
    
                                                                                                                    const [list, total] = await qb.getManyAndCount();
    
                                                                                                                    if (list.length) {
                                                                                                                      for (const item of list) {
                                                                                                                        const isHit = await this.cache.general('hget', 'article_hit', item.id);
    
                                                                                                                        if (isHit) {
                                                                                                                          item.hit = Number(isHit) + 1;
                                                                                                                        }
    
                                                                                                                        item.time = moment(item.time).format('YYYY-MM-DD');
                                                                                                                      }
                                                                                                                    }
                                                                                                                    return {
                                                                                                                      pageSize,
                                                                                                                      pageNum,
                                                                                                                      pages: Math.ceil(total / pageSize),
                                                                                                                      total,
                                                                                                                      list,
                                                                                                                    };*/

    //return `This action returns all adminuser`;
  }

  findOne(id: number) {
    return `This action returns a #${id} adminuser`;
  }

  async update(id: number, updateAdminuserDto: UpdateAdminuserDto) {
    console.log(updateAdminuserDto.radio);
    try {
      const data = await this.roleRepository.findOne({
        id: updateAdminuserDto.radio,
      });

      await this.usersRepository.update(id, { role: data });

      return `成功！`;
    } catch (e) {
      //  console.log(e);
      return '出错了';
    }
  }

  async remove(id: number) {
    await this.usersRepository.delete(id);
    return `成功！`;
  }
}

/*
@Injectable()
export class UsersService {
  constructor(
    @InjectRepository(UsersEntity)
    private readonly usersRepository: Repository<UsersEntity>,
  ) {}

  async findOne(username: string): Promise<UsersEntity> {
    return this.usersRepository
      .createQueryBuilder('user')
      .where('user.username = :username', { username: username })
      .addSelect('user.password')
      .getOne();
  }
  async save(Users) {
    let cUsers = new UsersEntity();
    cUsers = {
      ...cUsers,
      ...Users,
    };
    await this.usersRepository.save(cUsers);
    return {
      data: null,
      code: 200,
      msg: '成功了',
    };
  }
}
*/
